#ifdef MEM_DEBUG

#include <stdlib.h>
#include "Memory.h"
#include "AllocationManager.h"

#undef new

namespace Engine
{
	static int depth = 0;//recursion function prevents infinite recursion

	void* operator new(size_t size)
	{
		return operator new(size, "<no file>", 0);
	}

	void* operator new(size_t size, const char* fileName, int line)
	{
		void* address = malloc(size);

		++depth;

		if(depth == 1)
		{
			if(address)
			{
				TheAllocationManager::Instance() 
					->AddAllocation(Allocation(address, size, fileName, line));
			}
		}
		--depth;

		return address;
	}

	void operator delete(void* address)
	{
		++depth;

		if(depth == 1)
		{
			if(address)
			{
				TheAllocationManager::Instance() 
					->RemoveAllocation(address);
				free(address);
			}
		}
		--depth;
	}

	void IncrementDepth()
	{
		++depth;
	}
}

#endif
